package com.ziyueyuwen.xiniao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ziyueyuwen.xiniao.bean.ZyCourseInfo;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.query.QuestionQuery;
import com.ziyueyuwen.xiniao.bean.question.ZyQuestionCourseQuestion;
import com.ziyueyuwen.xiniao.bean.question.ZyQuestionData;
import com.ziyueyuwen.xiniao.bean.question.ZyQuestionDataPoint;
import com.ziyueyuwen.xiniao.bean.vo.ZyQuestionDataVO;
import com.ziyueyuwen.xiniao.dao.ZyQuestionDataMapper;
import com.ziyueyuwen.xiniao.dao.ZyQuestionDataPointMapper;
import com.ziyueyuwen.xiniao.dao.ZyQuestionPointMapper;
import com.ziyueyuwen.xiniao.enums.LessonModuleEnum;
import com.ziyueyuwen.xiniao.service.*;
import com.ziyueyuwen.xiniao.util.PageDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author wancheng  on 2019/1/10.
 */
@Service
public class QuestionDataServiceImpl implements QuestionDataService {

    @Autowired
    ZyQuestionDataMapper zyQuestionDataMapper;
    @Autowired
    ZyQuestionPointMapper zyQuestionPointMapper;
    @Autowired
    ZyQuestionDataPointMapper zyQuestionDataPointMapper;
    @Autowired
    QuestionCourseService questionCourseService;
    @Autowired
    SubjectQuestionService subjectQuestionService;
    @Autowired
    CourseService courseService;
    @Autowired
    PointService pointService;
    @Autowired
    MessageTaskService messageTaskService;

    @Override
    public ZyQuestionDataVO insert(ZyQuestionData data) {
        zyQuestionDataMapper.insertSelective(data);
        return getByPrimaryKey(data.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZyQuestionData insert(ZyQuestionData data, Set<Integer> points) {
        zyQuestionDataMapper.insertSelective(data);
        if (!CollectionUtils.isEmpty(points)){
            insertQuestionPoint(data.getId(),points);
        }
        return data;
    }
    private void insertQuestionPoint(Integer questionDataId,Set<Integer> pointIds){
        Set<Integer> parentIds = pointService.getPointParentIds(pointIds);
        List<ZyQuestionDataPoint> needInsertList = new ArrayList<>();
        parentIds.forEach(
                p -> {
                    ZyQuestionDataPoint insertParam = new ZyQuestionDataPoint();
                    insertParam.setQuestionDataId(questionDataId);
                    insertParam.setQuestionPointId(p);
                    needInsertList.add(insertParam);
                }
        );
        zyQuestionDataPointMapper.insertBatch(needInsertList);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZyQuestionDataVO insert(ZyQuestionData data, Set<Integer> points, Integer courseId,
                                   Integer lessonIndex,Integer questionGroup, String answerOrder) throws Exception {
        ZyQuestionData insertData = insert(data,points);
        ZyCourseInfo course = courseService.getByPreKey(courseId);
        if (course == null){
            throw new Exception("该课程不存在");
        }
        ZyQuestionCourseQuestion courseQuestion = new ZyQuestionCourseQuestion();
        courseQuestion.setQuestionDataId(insertData.getId());
        courseQuestion.setCourseId(courseId);
        courseQuestion.setLessonIndex(lessonIndex);
        courseQuestion.setLessonQuestionType(data.getLessonModule());
        courseQuestion.setCreateTime(new Date());
        if (questionGroup == null){
            courseQuestion.setQuestionGroup(0);
        }else{
            courseQuestion.setQuestionGroup(questionGroup);
        }
        questionCourseService.insert(courseQuestion);
        subjectQuestionService.saveSubjectQuestion(courseId, lessonIndex, answerOrder);
        return null;
    }

    @Override
    public ZyQuestionDataVO updateByPrimaryKey(ZyQuestionData data) {
        zyQuestionDataMapper.updateByPrimaryKeySelective(data);
        return getByPrimaryKey(data.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZyQuestionDataVO updateByPrimaryKey(ZyQuestionData data, Set<Integer> points) {
        zyQuestionDataMapper.updateByPrimaryKeySelective(data);
        if (!CollectionUtils.isEmpty(points)) {
            ZyQuestionDataPoint deleteParam = new ZyQuestionDataPoint();
            deleteParam.setQuestionDataId(data.getId());
            zyQuestionDataPointMapper.delete(deleteParam);
            insertQuestionPoint(data.getId(),points);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZyQuestionDataVO updateSyncCourse(ZyQuestionData data, Set<Integer> points, Integer courseId,
                                             Integer lessonIndex, String answerOrder) throws Exception {
        if (!LessonModuleEnum.CLASSROOM_TEST.equals(data.getLessonModule()) &&
                !CollectionUtils.isEmpty(messageTaskService.getByCourseIdAndLessonIndex(courseId,lessonIndex,data.getLessonModule()))){
            throw new Exception("当前作业已发布,无法修改");
        }
        this.updateByPrimaryKey(data,points);
        subjectQuestionService.saveSubjectQuestion(courseId, lessonIndex, answerOrder);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByPrimaryKey(Integer id) {
        zyQuestionDataMapper.deleteByPrimaryKey(id);
        ZyQuestionDataPoint deleteParam = new ZyQuestionDataPoint();
        deleteParam.setQuestionDataId(id);
        zyQuestionDataPointMapper.delete(deleteParam);
    }

    @Override
    public ZyQuestionDataVO getByPrimaryKey(Integer id) {
        ZyQuestionDataVO data = zyQuestionDataMapper.getQuestionDataByQuestionId(id);
        return data;
    }

    @Override
    public PageData getQuestion(QuestionQuery query) {
        PageHelper.startPage(query.getPage(), query.getRows());
        PageHelper.orderBy(" data.create_time desc ");
        List<ZyQuestionDataVO> questionData = zyQuestionDataMapper.getQuestionDataForPageQuery(query);
        PageInfo<ZyQuestionDataVO> pageInfo = new PageInfo<>(questionData);
        PageData<ZyQuestionDataVO> pageData = PageDataUtil.getPageData(pageInfo);
        pageData.getData().forEach(
                q->{
                    q.setGradeValue(q.getGrade().getDescription());
                }
        );
        return pageData;
    }
}
